#include "max17043.h"
#include "main.h"
#include "myiic.h" 

_max17043_data max17043_data;

//初始化max17043触摸屏
//返回值:0,初始化成功;1,初始化失败 
esp_err_t max17043_init(void)
{
    esp_err_t ret =0;
	int temp=0;
	u8 buf[2];
	uint8_t ack=0;


	buf[0]=0x54;
	buf[1]=0x00;
	ack = MAX17043_WR_Reg(MAX17043_COMMAND,buf,2);
	if(ack==0)
	{
		max17043_DEBUG("写入成功\r\n");

	}else
	{
		max17043_DEBUG("写入失败\r\n");
	}
	
	vTaskDelay(100 / portTICK_PERIOD_MS);


	MAX17043_RD_Reg(MAX17043_CONFIG,buf,2);
	temp = buf[0]<<8|buf[1];
	if(buf[0]==0x97&&buf[1]==0x1c)
	{

		max17043_DEBUG("初始化成功:0x%x\r\n",temp);
	}else
	{
		max17043_DEBUG("初始化失败:0x%x\r\n",temp);
	}


	buf[0]=0x40;
	buf[1]=0x00;
	ack = MAX17043_WR_Reg(MAX17043_MODE,buf,2);
	if(ack==0)
	{
		max17043_DEBUG("写入成功\r\n");

	}else
	{
		max17043_DEBUG("写入失败\r\n");
	}


	buf[0]=0x97;
	buf[1]=0x00;
	ack = MAX17043_WR_Reg(MAX17043_CONFIG,buf,2);
	if(ack==0)
	{
		max17043_DEBUG("写入成功\r\n");

	}else
	{
		max17043_DEBUG("写入失败\r\n");
	}




     
    xTaskCreate(max17043_test_task,"max17043_test_task",1024*2,NULL,20,NULL);
    
    return ret;
}

void max17043_test_task(void *pvParameter)
{
	uint8_t buf[20];
	float temp=0;
	while (1) 
    {
		// MAX17043_setWakeUp();
		// vTaskDelay(1000 / portTICK_PERIOD_MS); 
		// MAX17043_clearInterrupt();
		// vTaskDelay(1000 / portTICK_PERIOD_MS); 
		printf("-----\r\n");
		// MAX17043_RD_Reg(0x00,buf,20);
		// for(int i=0;i<14;i++)
		// {
		// 	printf("buf[0x%x]=0x%x\r\n",i,buf[i]);
		// }

		temp = MAX17043_readVoltage();
		printf("电压:%0.3fmv\r\n",temp);

		temp = MAX17043_readPercentage();
		printf("电量:%0.3f%%\r\n",temp);
		
		printf("-----\r\n");

        vTaskDelay(2000 / portTICK_PERIOD_MS); 
 	}
	vTaskDelete(NULL);
}

void MAX17043_writeRegBits(uint8_t reg, uint16_t dat, uint16_t bits, uint8_t offset) 
{
	uint16_t        temp;
	uint8_t buf[2];
	MAX17043_RD_Reg(MAX17043_VCELL,buf,2);
	temp = buf[0]|buf[1];

	temp = (temp & (~(bits << offset))) | (dat << offset);

	buf[0]=(temp>>8)&0xff;
	buf[1]=temp&0xff;
	MAX17043_WR_Reg(MAX17043_CONFIG,buf,2);	

}

void MAX17043_setSleep()
{
  MAX17043_writeRegBits(MAX17043_CONFIG, 1, 0x01, 7);
}

void MAX17043_setWakeUp()
{
  MAX17043_writeRegBits(MAX17043_CONFIG, 0, 0x01, 7);
}

void MAX17043_setInterrupt(uint8_t per)
{
  uint16_t temp=0;

  if(per > 32)
    temp = 32;
  else if(per < 1)
    temp = 1;
  temp = 32 - temp;
  MAX17043_writeRegBits(MAX17043_CONFIG, temp, 0x01f, 0);
}

void MAX17043_clearInterrupt()
{
	MAX17043_writeRegBits(MAX17043_CONFIG, 0, 0x01, 5);

}

float MAX17043_readVoltage()
{
	uint8_t buf[2];
	uint16_t  per =0;
	MAX17043_RD_Reg(MAX17043_VCELL,buf,2);
	per = buf[0]|buf[1];
	if(per==0)
		return 0;

	return (1.25f * (float)(per >> 4));
}

float MAX17043_readPercentage()
{
	uint8_t buf[2];
	uint16_t  per =0;
	MAX17043_RD_Reg(MAX17043_SOC,buf,2);
	per = buf[0]|buf[1];
	if(per==0)
		return 0;	
	return (float)((per >> 8) + 0.003906f * (per & 0x00ff));
}


u8 Max17043_Read_Byte(u8 add,uint8_t regadd,u8 *data)
{
	u8 IIC_bufferH=0x00;
	u8 IIC_bufferL=0x00;	
	u8 vol=0x00;
	u8 ack=0;
	u8 ret=0;
	IIC_Start();
	IIC_Send_Byte(add);
	ack = IIC_Wait_Ack();
	if(ack)
	{
		//max17043_DEBUG("应答失败0\r\n");
		ret=1;
	}else
	{
		//max17043_DEBUG("应答成功0\r\n");
	}			
	IIC_Send_Byte(regadd);
	ack = IIC_Wait_Ack();
	if(ack)
	{
		//max17043_DEBUG("应答失败1\r\n");
		ret=1;
	}else
	{
		//max17043_DEBUG("应答成功1\r\n");
	}	
	IIC_Start();
	IIC_Send_Byte(add|0x01);
	ack = IIC_Wait_Ack();
	if(ack)
	{
		//max17043_DEBUG("应答失败2\r\n");
		ret=1;
	}else
	{
		//max17043_DEBUG("应答成功2\r\n");
	}	
	*data=IIC_Read_Byte(0);
	IIC_Stop();	
	return ret;
}


u8  MAX17043_RD_Reg(u16 reg,u8 *buf,u8 len)
{
	u8 ret=0;
	u8 i; 
 	IIC_Start();	
 	IIC_Send_Byte(MAX17043_CMD_WR);   	//发送写命令 	 
	IIC_Wait_Ack(); 	 										  		   
 	IIC_Send_Byte(reg&0XFF);   	//发送低8位地址
	IIC_Wait_Ack();  
 	IIC_Start();  	 	   
	IIC_Send_Byte(MAX17043_CMD_RD);   	//发送读命令		   
	ret = IIC_Wait_Ack();	   
	for(i=0;i<len;i++)
	{	   
    	buf[i]=IIC_Read_Byte(i==(len-1)?0:1); //发数据	  
	} 
    IIC_Stop();//产生一个停止条件 
	return ret;    
}

u8 MAX17043_WR_Reg(u16 reg,u8 *buf,u8 len)
{
	u8 i;
	u8 ret=0;
	IIC_Start();	 
	IIC_Send_Byte(MAX17043_CMD_WR);	//发送写命令 	 
	IIC_Wait_Ack(); 	 										  		   
	IIC_Send_Byte(reg&0XFF);   	//发送低8位地址
	IIC_Wait_Ack();  
	for(i=0;i<len;i++)
	{	   
    	IIC_Send_Byte(buf[i]);  	//发数据
		ret=IIC_Wait_Ack();
		if(ret)
		{			
			break; 
		} 
	}
    IIC_Stop();					//产生一个停止条件	    
	return ret; 
}
